package com.project.personalfundmanager.client.common;

import java.util.List;

import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.project.personalfundmanager.client.taxitems.ParentItem;
import com.project.personalfundmanager.shared.models.StringWrapperModel;

@SuppressWarnings( "unchecked" )
public class UIHelper
{
	private ParentItem tabItem;

	public UIHelper( ParentItem tabItem )
	{
		this.tabItem = tabItem;
	}

	public NumberField getNumberField( String lable, String name, boolean isEditable )
	{
		return getNumberField( lable, name, isEditable, 0 );
	}

	public NumberField getNumberField( String label, String name, boolean isEditable, final int limit )
	{
		return new InputField( tabItem ).getInputField( label, name, isEditable, limit );
	}

	public ComboBox<StringWrapperModel> getCombobox( List<StringWrapperModel> values, String name, String label, boolean isEditable )
	{
		ComboBox<StringWrapperModel> comboBox = new ComboBox<StringWrapperModel>();
		ListStore<StringWrapperModel> store = new ListStore<StringWrapperModel>();
		store.add( values );
		comboBox.setStore( store );
		comboBox.setEmptyText("Select a value...");  
		comboBox.setDisplayField( "value" );
		comboBox.setTypeAhead(true);
		comboBox.setAllowBlank( false );
		comboBox.setName( name );
		comboBox.setFieldLabel( label );
		comboBox.setEditable( isEditable );
		comboBox.setMessageTarget("tooltip");
		comboBox.setValue( values.get( 0 ) );
		comboBox.addListener( Events.OnChange, new Listener<FieldEvent>()
		{
			public void handleEvent( FieldEvent be )
			{
				if(tabItem != null)
					tabItem.calculateTotal();
			}
		} );
		return comboBox;
	}
}
